<?php
/**
* Products Categories Rank Switch
*/
if (!defined("EXPONENT")) exit("");
$obj=null;
$value_rank=0;
if (exponent_permissions_check(array("administrate", "configure"),$loc) && isset($_REQUEST['stype'])) {
    $id=0;
    if(isset($_REQUEST['id']))
    {
        $id = intval($_REQUEST['id']);
    }
    $lc = serialize($loc);
    $cat=$db->selectObject("bannermediamodule_item","id = {$id} and location_data='".serialize($loc)."'");
    $value_rank = $cat->rank;
    if ($cat && ($cat->rank > 0))
    {
        
        // get the other item that we need to switch
        //$sql_where = "AND rank < {$cat->rank}"; // move up
        if ($_REQUEST['stype'] == 1) // move down
        {
            $temp = $db->selectObject("bannermediamodule_item","location_data='".serialize($loc)."' AND rank > {$value_rank} ORDER BY rank DESC");            
            //$sql_where="AND rank > {$cat->rank}";
        }
        else
        {
             $temp = $db->selectObject("bannermediamodule_item","location_data='".serialize($loc)."' AND rank < {$value_rank} ORDER BY rank DESC");   
        }
        //$sql_where.=" ORDER BY rank DESC";
        //$temp=$db->selectObject("productsmodule_country", "location_data = '{$cat->location_data}'  AND id = {$cat->id}{$sql_where}");
        if ($temp)
        {
            $temp_value=$temp->rank;
            $temp->rank=$cat->rank;
            $cat->rank=$temp_value;
            $db->updateObject($temp,"bannermediamodule_item");
            $db->updateObject($cat,"bannermediamodule_item");
            //$obj->id=$cat->parent_id; // parent id to reload :)
        }
    }
} else {
    //echo SITE_403_HTML;
}
exponent_flow_redirect(SYS_FLOW_ACTION);
?>
